using B.S.XZYData.Api.Wrter.Application.Command.ZSH;
using B.S.XZYData.Domain.ZSH;
using B.S.XZYData.ErrorCode;
using B.S.XZYData.Infrastructure.Interface;
using MediatR;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Wrter.Application.Handler.ZSH
{
	/// <summary>
	/// 批量删除不良品项处理器（伪删除）
	/// </summary>
	public class DeleteDefectiveBatchHandler : IRequestHandler<DeleteDefectiveBatchCommand, ApiResult<int>>
	{
		private readonly IBaseRepository<Defective> _repo;

		public DeleteDefectiveBatchHandler(IBaseRepository<Defective> repo)
		{
			_repo = repo;
		}

		public async Task<ApiResult<int>> Handle(DeleteDefectiveBatchCommand request, CancellationToken cancellationToken)
		{
			var res = new ApiResult<int>();
			if (request.Ids == null || request.Ids.Count == 0)
			{
				res.Code = ApiEnum.Fail;
				res.Msg = "请选择要删除的数据";
				return await Task.FromResult(res);
			}

			var list = _repo.GetAll().Where(x => request.Ids.Contains(x.Id)).ToList();
			foreach (var item in list)
			{
				item.IsDeleted = true;
			}
			var count = await _repo.UpdateBatchAsync(list);

			res.Code = ApiEnum.Success;
			res.Msg = "删除成功";
			res.Data = count;
			return res;
		}
	}
}


